﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls.Primitives;
using System.Windows.Controls;
using iTeachSoft.Code.Managers;
using iTeachSoft.Presentation.Code.Utility;
using System.Windows;
using iTeachSoft.CoreObjects;
using iTeachSoft.Presentation.Code.Managers;

namespace iTeachSoft.Presentation.Code.Wrappers
{
    public class BasicShape : Thumb
    {
        private bool mIsDragged = false;
        protected BaseSlideObject mCoreObject = null;

        public BasicShape()
        {
            Canvas.SetLeft(this, 1);
            Canvas.SetTop(this, 1);

            this.DragStarted += new DragStartedEventHandler(BasicShape_DragStarted);
            this.DragCompleted += new DragCompletedEventHandler(BasicShape_DragCompleted);
            this.DragDelta += new DragDeltaEventHandler(BasicShape_DragDelta);

            this.MouseDown += new System.Windows.Input.MouseButtonEventHandler(BasicShape_MouseDown);
        }

        void BasicShape_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            RuntimeStorage.CurrentSelectedShape = mCoreObject;
        }

        private bool OKToMove(double xOffset, double yOffset)
        {
            return RuntimeStorage.SlideViewControlBounds.Contains(this.GetBounds(xOffset, yOffset));
        }

        public Rect GetBounds()
        {
            return GraphicsUtility.GetBounds(this, Application.Current.MainWindow);
        }

        public Rect GetBounds(double xOffset, double yOffset)
        {
            var result = GetBounds();
            result.Offset(xOffset, yOffset);

            return result;
        }

        void BasicShape_DragDelta(object sender, DragDeltaEventArgs e)
        {
            if (mIsDragged && OKToMove(e.HorizontalChange, e.VerticalChange))
            {
                Canvas.SetLeft(this, Canvas.GetLeft(this) + e.HorizontalChange);
                Canvas.SetTop(this, Canvas.GetTop(this) + e.VerticalChange);

                mCoreObject.Bounds = new System.Drawing.Rectangle(mCoreObject.Bounds.X + (int)e.HorizontalChange, mCoreObject.Bounds.Y + (int)e.VerticalChange, mCoreObject.Bounds.Width, mCoreObject.Bounds.Height);
            }
        }

        void BasicShape_DragCompleted(object sender, DragCompletedEventArgs e)
        {
            mIsDragged = false;

            EventsManagement.Instance.OnReflectSlideViewerRequested(this, EventArgs.Empty);
        }

        void BasicShape_DragStarted(object sender, DragStartedEventArgs e)
        {
            mIsDragged = true;
        }

    }
}
